package com.freeLearning.boot.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freeLearning.boot.core.security.util.SecurityUtils;
import com.freeLearning.boot.course.enums.StatusEnum;
import com.freeLearning.boot.course.mapper.CourseInfoMapper;
import com.freeLearning.boot.course.model.VO.CourseInfoPageVO;
import com.freeLearning.boot.course.model.entity.CourseAudit;
import com.freeLearning.boot.course.model.entity.CourseAuditHistory;
import com.freeLearning.boot.course.model.entity.CourseInfo;
import com.freeLearning.boot.course.model.query.CourseInfoPageQuery;
import com.freeLearning.boot.course.service.CourseAuditHistoryService;
import com.freeLearning.boot.course.service.CourseAuditService;
import com.freeLearning.boot.course.service.CourseInfoService;
import com.freeLearning.boot.system.model.bo.UserBO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class CourseInfoServiceImpl extends ServiceImpl<CourseInfoMapper, CourseInfo> implements CourseInfoService {
    private final CourseInfoMapper courseInfoMapper;
    private final CourseAuditService courseAuditService;
    private final CourseAuditHistoryService courseAuditHistoryService;
    @Override
    public IPage<CourseInfoPageVO> pageQuery(CourseInfoPageQuery courseInfoPageQuery) {
        // 参数构建
        int pageNum = courseInfoPageQuery.getPageNum();
        int pageSize = courseInfoPageQuery.getPageSize();
        Page<CourseInfo> page = new Page<>(pageNum, pageSize);
        return this.courseInfoMapper.getPage(page, courseInfoPageQuery);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveCourseInfo(CourseInfo courseInfo) {
        courseInfoMapper.insert(courseInfo);
        CourseAudit courseAudit = new CourseAudit();
        courseAudit.setStatus(StatusEnum.WAIT_AUDIT.getValue());
        courseAudit.setCourseId(courseInfo.getId());
        courseAudit.setAuditorId(SecurityUtils.getUserId());
        courseAudit.setAuditorName(courseInfo.getConsultantName());
        courseAudit.setOpinion("课程新增初审");
        courseAuditHistoryService.save(BeanUtil.copyProperties(courseAudit, CourseAuditHistory.class));
        return courseAuditService.save(courseAudit);
    }

}
